pub fn time_info(time: &str) -> String {
    let (year, month, day) = parse_date(time);
    let week_of_year = calculate_week_of_year(year, month, day);
    let days_left_in_year = calculate_days_left_in_year(year, month, day);
    let days_until_chinese_new_year = calculate_days_until_chinese_new_year(year, month, day);

    format!("{},{},{}", week_of_year, days_left_in_year, days_until_chinese_new_year)
}

fn parse_date(time: &str) -> (i32, i32, i32) {
    let parts: Vec<i32> = time.split('-').map(|s| s.parse().unwrap()).collect();
    (parts[0], parts[1], parts[2])
}

fn is_leap_year(year: i32) -> bool {
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

fn days_in_month(year: i32, month: i32) -> i32 {
    match month {
        1 => 31,
        2 => if is_leap_year(year) { 29 } else { 28 },
        3 => 31,
        4 => 30,
        5 => 31,
        6 => 30,
        7 => 31,
        8 => 31,
        9 => 30,
        10 => 31,
        11 => 30,
        12 => 31,
        _ => 0,
    }
}

fn calculate_day_of_year(year: i32, month: i32, day: i32) -> i32 {
    let mut day_of_year = 0;
    for m in 1..month {
        day_of_year += days_in_month(year, m);
    }
    day_of_year + day
}

fn calculate_week_of_year(year: i32, month: i32, day: i32) -> i32 {
    let day_of_year = calculate_day_of_year(year, month, day);
    let dow = get_day_of_week(year, month, day);
    let _dow_iso = if dow == 0 { 7 } else { dow }; // Convert Sunday=0 to Sunday=7

    let (weeks_in_year,first_week_day) = total_weeks_in_year_and_first_week_start(year);
    println!("weeks_in_year: {}, first_week_day: {}", weeks_in_year, first_week_day);
    let  week_number = (day_of_year + 6 - first_week_day) / 7 ;

    if week_number < 1 {
        weeks_in_year
    } else if week_number > weeks_in_year {
        1
    } else {
        week_number
    }
}
fn total_weeks_in_year_and_first_week_start(year: i32) -> (i32,i32) {
    // A year has 53 weeks if:
    // - January 1 is a Thursday
    // - Or it's a leap year and January 1 is a Wednesday
    let _jan1_dow = get_day_of_week(year, 1, 1);

    // println!("_jan1_dow: {}", _jan1_dow);
    if _jan1_dow == 3 || (is_leap_year(year) && _jan1_dow == 2) {
        (53, -4)
    } else if _jan1_dow > 3 {
        (53, 7 - _jan1_dow)
    } else {
        (52, -_jan1_dow)
    }
}

fn calculate_days_left_in_year(year: i32, month: i32, day: i32) -> i32 {
    let total_days_in_year = if is_leap_year(year) { 366 } else { 365 };
    let day_of_year = calculate_day_of_year(year, month, day);
    total_days_in_year - day_of_year
}

fn calculate_days_until_chinese_new_year(year: i32, month: i32, day: i32) -> i32 {
    if let Some((cny_month, cny_day)) = get_chinese_new_year(year) {
        let day_of_year = calculate_day_of_year(year, month, day);
        let cny_day_of_year = calculate_day_of_year(year, cny_month, cny_day);

        if day_of_year < cny_day_of_year {
            cny_day_of_year - day_of_year - 1
        } else {
            let days_left_in_year = calculate_days_left_in_year(year, month, day);
            if let Some((next_cny_month, next_cny_day)) = get_chinese_new_year(year + 1) {
                let next_cny_day_of_year = calculate_day_of_year(year + 1, next_cny_month, next_cny_day);
                days_left_in_year + next_cny_day_of_year - 1
            } else {
                0
            }
        }
    } else {
        0
    }
}

// Zeller's Congruence to calculate the day of the week
fn get_day_of_week(year: i32, month: i32, day: i32) -> i32 {
    let mut y = year;
    let mut m = month;
    if m < 3 {
        m += 12;
        y -= 1;
    }
    let k = y % 100;
    let j = y / 100;
    let h = (day + (13 * (m + 1)) / 5 + k + (k / 4) + (j / 4) + (5 * j)) % 7;
    (h + 5) % 7 // Adjust to: 0 = Sunday, 1 = Monday, ..., 6 = Saturday
}


fn get_chinese_new_year(year: i32) -> Option<(i32, i32)> {
    const CHINESE_NEW_YEAR: &[(i32, i32, i32)] = &[
        (1900, 1, 31), (1901, 2, 19), (1902, 2, 8), (1903, 1, 29), (1904, 2, 16),
        (1905, 2, 4), (1906, 1, 25), (1907, 2, 13), (1908, 2, 2), (1909, 1, 22),
        (1910, 2, 10), (1911, 1, 30), (1912, 2, 18), (1913, 2, 6), (1914, 1, 26),
        (1915, 2, 14), (1916, 2, 3), (1917, 1, 23), (1918, 2, 11), (1919, 2, 1),
        (1920, 2, 20), (1921, 2, 8), (1922, 1, 28), (1923, 2, 16), (1924, 2, 5),
        (1925, 1, 24), (1926, 2, 13), (1927, 2, 2), (1928, 1, 23), (1929, 2, 10),
        (1930, 1, 30), (1931, 2, 17), (1932, 2, 6), (1933, 1, 26), (1934, 2, 14),
        (1935, 2, 4), (1936, 1, 24), (1937, 2, 11), (1938, 1, 31), (1939, 2, 19),
        (1940, 2, 8), (1941, 1, 27), (1942, 2, 15), (1943, 2, 5), (1944, 1, 25),
        (1945, 2, 13), (1946, 2, 2), (1947, 1, 22), (1948, 2, 10), (1949, 1, 29),
        (1950, 2, 17), (1951, 2, 6), (1952, 1, 27), (1953, 2, 14), (1954, 2, 3),
        (1955, 1, 24), (1956, 2, 12), (1957, 1, 31), (1958, 2, 18), (1959, 2, 8),
        (1960, 1, 28), (1961, 2, 15), (1962, 2, 5), (1963, 1, 25), (1964, 2, 13),
        (1965, 2, 2), (1966, 1, 21), (1967, 2, 9), (1968, 1, 30), (1969, 2, 17),
        (1970, 2, 6), (1971, 1, 27), (1972, 2, 15), (1973, 2, 3), (1974, 1, 23),
        (1975, 2, 11), (1976, 1, 31), (1977, 2, 18), (1978, 2, 7), (1979, 1, 28),
        (1980, 2, 16), (1981, 2, 5), (1982, 1, 25), (1983, 2, 13), (1984, 2, 2),
        (1985, 2, 20), (1986, 2, 9), (1987, 1, 29), (1988, 2, 17), (1989, 2, 6),
        (1990, 1, 27), (1991, 2, 15), (1992, 2, 4), (1993, 1, 23), (1994, 2, 10),
        (1995, 1, 31), (1996, 2, 19), (1997, 2, 7), (1998, 1, 28), (1999, 2, 16),
        (2000, 2, 5), (2001, 1, 24), (2002, 2, 12), (2003, 2, 1), (2004, 1, 22),
        (2005, 2, 9), (2006, 1, 29), (2007, 2, 18), (2008, 2, 7), (2009, 1, 26),
        (2010, 2, 14), (2011, 2, 3), (2012, 1, 23), (2013, 2, 10), (2014, 1, 31),
        (2015, 2, 19), (2016, 2, 8), (2017, 1, 28), (2018, 2, 16), (2019, 2, 5),
        (2020, 1, 25), (2021, 2, 12), (2022, 2, 1), (2023, 1, 22), (2024, 2, 10),
        (2025, 1, 29), (2026, 2, 17), (2027, 2, 6), (2028, 1, 26), (2029, 2, 13),
        (2030, 2, 3), (2031, 1, 23), (2032, 2, 11), (2033, 1, 31), (2034, 2, 19),
        (2035, 2, 8), (2036, 1, 28), (2037, 2, 15), (2038, 2, 4), (2039, 1, 24),
        (2040, 2, 12), (2041, 2, 1), (2042, 1, 22), (2043, 2, 10), (2044, 1, 30),
        (2045, 2, 17), (2046, 2, 6), (2047, 1, 26), (2048, 2, 14), (2049, 2, 2),
        (2050, 1, 23), (2051, 2, 11), (2052, 2, 1), (2053, 2, 19), (2054, 2, 8),
        (2055, 1, 28), (2056, 2, 15), (2057, 2, 4), (2058, 1, 24), (2059, 2, 12),
        (2060, 2, 2), (2061, 1, 21), (2062, 2, 9), (2063, 1, 29), (2064, 2, 17),
        (2065, 2, 5), (2066, 1, 26), (2067, 2, 14), (2068, 2, 3), (2069, 1, 23),
        (2070, 2, 11), (2071, 1, 31), (2072, 2, 19), (2073, 2, 7), (2074, 1, 27),
        (2075, 2, 15), (2076, 2, 5), (2077, 1, 24), (2078, 2, 12), (2079, 2, 2),
        (2080, 1, 22), (2081, 2, 9), (2082, 1, 29), (2083, 2, 17), (2084, 2, 6),
        (2085, 1, 26), (2086, 2, 14), (2087, 2, 3), (2088, 1, 24), (2089, 2, 10),
        (2090, 1, 30), (2091, 2, 18), (2092, 2, 7), (2093, 1, 27), (2094, 2, 15),
        (2095, 2, 5), (2096, 1, 25), (2097, 2, 12), (2098, 2, 1), (2099, 1, 21),
        (2100, 2, 9),
    ];

    for &(y, m, d) in CHINESE_NEW_YEAR.iter() {
        if y == year {
            return Some((m, d));
        }
    }
    None
}
 
